package project.taobao.commons.activemq.template;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import project.taobao.commons.activemq.consumer.Consumer;
import project.taobao.commons.activemq.factory.ConsumerFactory;
import project.taobao.commons.activemq.factory.ProducerFactory;
import project.taobao.commons.activemq.send.Producer;
import project.taobao.commons.activemq.utils.ActiveMqUtils;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageListener;


public class ActiveMqTemplate {

    // methods
    public static void sendMessage(String queueName, Object message) {
        ProducerFactory pf = ActiveMqUtils.getProducerFactory();
        Producer producer = pf.createProducer();
        Destination queue = ActiveMqUtils.getDestination(queueName);
        producer.sendMessage(queue, message);
    }

    public static Object consumerMessage(String queueName) {
        ConsumerFactory cf = ActiveMqUtils.getConsumerFactory();
        Consumer consumer = cf.createConsumer();
        Destination queue = ActiveMqUtils.getDestination(queueName);
        consumer.consumerMessage(queue);
        return consumer.getInfomation();
    }

    public static DefaultMessageListenerContainer getDefaultMessageListenerContainer(Destination destination){
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setConnectionFactory(ActiveMqUtils.getConnectionFactory());
        container.setDestination(destination);
        return container;
    }
    // getters, setters

    // constructors
}
